package com.alibaba.citrus.cr.price.ability.functions.factory.price.process;

import com.alibaba.acm.shaded.com.alibaba.metrics.StringUtils;
import com.alibaba.citrus.cr.common.annotation.RepositoryInvoker;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.crplatformenhance.api.customerquery.service.CustomerQueryService;
import com.epoch.app.crplatformenhance.dto.request.CustomerBatchRequest;
import com.epoch.app.crplatformenhance.sdo.CustomerSDO;
import com.epoch.app.crpricecenter.enums.DimensionPropertyEnum;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author hanguo
 * @date 2021/11/19
 * @Description 售达方
 */
@Component
public class SaleAddressProcess extends PriceProcess{


    /**
     * 售达方地址查询服务
     */
    @Resource
    private CustomerQueryService customerQueryService;


    @Override
    @RepositoryInvoker
    public Map<String, String> obtainCodeAndDescMap(List<String> codeList, boolean innerCode) {
        List<CustomerSDO> customerSDOList = obtainCustomerSDOList(codeList, innerCode);
        if (CollectionUtils.isEmpty(customerSDOList)) {
            return new HashMap<>();
        }

        if (innerCode) {
            customerSDOList.stream().filter(item -> StringUtils.
                    isNotBlank(item.getCode()) && StringUtils.
                    isNotBlank(item.getName())).collect(Collectors.
                    toMap(CustomerSDO::getCode, CustomerSDO::getName, (x, y) -> x));
        }
        return customerSDOList.stream().filter(item -> StringUtils.
                isNotBlank(item.getOutCode()) && StringUtils.
                isNotBlank(item.getName())).collect(Collectors.
                toMap(CustomerSDO::getOutCode, CustomerSDO::getName, (x, y) -> x));

    }

    @Override
    @RepositoryInvoker
    public Map<String, String> obtainInnerCodeAndOuterCodeMap(List<String> codeList, boolean innerCode) {
        List<CustomerSDO> customerSDOList = obtainCustomerSDOList(codeList, innerCode);
        if (CollectionUtils.isEmpty(customerSDOList)) {
            return new HashMap<>();
        }

        if (innerCode) {
            customerSDOList.stream().filter(item -> StringUtils.
                    isNotBlank(item.getCode()) && StringUtils.
                    isNotBlank(item.getOutCode())).collect(Collectors.
                    toMap(CustomerSDO::getCode, CustomerSDO::getOutCode, (x, y) -> x));
        }
        return customerSDOList.stream().filter(item -> StringUtils.
                isNotBlank(item.getCode()) && StringUtils.
                isNotBlank(item.getOutCode())).collect(Collectors.
                toMap(CustomerSDO::getOutCode, CustomerSDO::getCode, (x, y) -> x));
    }

    @RepositoryInvoker
    public List<CustomerSDO> obtainCustomerSDOList(List<String> codeList, boolean innerCode){
        CustomerBatchRequest customerBatchRequest = new CustomerBatchRequest();
        customerBatchRequest.setCodes(codeList);
        Result<List<CustomerSDO>> listResult = customerQueryService.batchQueryCustomer(customerBatchRequest);

        if (CollectionUtils.isEmpty(listResult.getResult())) {
            return new ArrayList<>();
        }

        return listResult.getResult();
    }

    @Override
    public String getPropertyCode() {
        return DimensionPropertyEnum.CUSTOMER_SALE_ADDRESS_CODE.getKey();
    }
}
